<template>
	<view class="main">
		<u-navbar title="我的订单" :autoBack="true" :placeholder="true" :titleStyle="state.titleStyle" :height="40"
			bgColor="#fff">
		</u-navbar>
		<view class="tanNav">
			<view :class="state.navid==val.id?'onesmo':'ones'" v-for="(val,index) in state.navList" :key='index' @click="getNav(val.id)">
				{{val.name}}
				<view class="text"></view>
			</view>
		</view>
		<view class="center">
			<view class="lieones" v-for="(item,index) in state.list" >
				<view class="topen">
					<text class="hao">订单编号 {{item.number}}</text>
					<text class="types" v-if="item.status_text=='待付款'||item.status_text=='待签收'">{{item.status_text}}</text>
					<text class="wancheng" v-else>{{item.status_text}}</text>
				</view>
				<view class="tuDing" @click="sheep.$router.go('/pages/user/orderDetail?id='+item.id+'&orderId='+state.orderId)">
					<view class="imges">
						<image :src="item.show_image" mode=""></image>
					</view>
					<view class="rights">
						<view class="tites">
							{{item.title}}
						</view>
						<view class="titemo">
							<view class="">
								￥{{item.amount}}
							</view>
							<view class="tuikuan" v-if="item.status==4">退款成功</view>
						</view>
					</view>
				</view>
				<view class="orderDing">
					<view class="quXiao" v-if="item.status_text=='已退款'||item.status_text=='待付款'&& state.orderId==1" @click="sOrder(index,'cancellation')">
						取消订单
					</view>
					<view class="xiuJia" v-if="item.status_text=='待付款' && state.orderId==2" @click="showPrice(index)">
						修改价格
					</view>
					<view class="xiuJia" v-if="item.status_text=='待付款' && state.orderId==1" @click="zhifu(index)">
						立即支付
					</view>
					
					<view class="xiuJia" v-if="item.status_text=='待签收' && state.orderId==1"  @click="sOrder(index,'confirm')">
						立即签收
					</view>
					<view class="xiuJia" v-if="item.status_text=='待签收' && state.orderId==2"  @click="getQian(index)">
					    提醒签收
					</view>
					<view class="tui" v-if="item.status_text=='待签收' && state.orderId==1" @click="sOrder(index,'refund')">
						退款
					</view>
				</view>
				
			</view>
		</view>
		<u-popup :show="state.showPrice" mode="bottom"  @close="close">
			<view class="priceBox">
				<view class="input">
					<input type="number" placeholder="请输入修改的价格" v-model="state.amount"/>
				</view>
				<view class="isPrice" @click="setPrice">确认</view>
			</view>
		</u-popup>
	</view>
</template>

<script setup>
	import {reactive} from 'vue';
	import sheep from '@/sheep';
	import {
		onLoad,
	} from '@dcloudio/uni-app';
	const state = reactive({
		showPrice:false,
		price:'',
		order:'',
		titleStyle: 'fontWeight:bold',
		navList:[
			{
				id:0,
				name:'待付款'
			},
			{
				id:1,
				name:'待签收'
			},
			{
				id:2,
				name:'已完成'
			},
			{
				id:3,
				name:'已取消'
			},
			{
				id:4,
				name:'已退款'
			}
		],
		navid:0,
		amount:'',
		list:[],
		pageSize:10,
		page:1,
		hasMore:true,
		isLoad:false,
		index:'',
		istuan:'',
		orderId:'',//1是个人订单2是团队订单
	})
	onLoad((options)=>{
		state.orderId=options.id
		if(options.status){
		   state.navid=options.status
		}
		if(options.id == 1){
			state.order_type=2
		}else{
			state.order_type=3
		}
		newsDatas()
		// getUserinfo()
	})
	function showPrice(e){
		state.index = e
		state.showPrice=true
	}
	// 提醒签收
	async function getQian(index){
		let userId=state.list[index].user_id
		let fabu_id=state.list[index].fabu_id
		let channel=state.list[index].channel
		const {code,data} = await sheep.$api.zqzApi.addCon({
			token:uni.getStorageSync('token'),
			content:'如订单无误请您确认签收',
			receive_user_id:userId,
			type:'send',
			channel:channel,
			is_chat:'1',
			fabu_id:fabu_id,
		})
		if(code=='1'){
			uni.showToast({
			  title:"发送成功",
			  icon: "none",
			  duration: 1000,
			})
		}
	}
	async function getUserinfo(){
		const {code,data} = await sheep.$api.user.userInfo({
			token:uni.getStorageSync('token'),
		})
		if(code=='1'){
			state.istuan = data.auth_type
		}
	}
	async function sOrder(index,type){
		
		if(type=='confirm'){
			uni.showModal({
				title: '确认签收订单',
				content: '你确定要签收订单吗？',
				success() {
					setorder(index,type)
				}
			})
		}
		if(type=='cancellation'){
			uni.showModal({
				title: '确认取消订单',
				content: '你确定要取消订单吗？',
				success() {
					setorder(index,type)
				}
			})
		}
		if(type=='refund'){
			uni.showToast({
			  title: '申请证书中，请稍后申请退款',
			  icon: 'none',
			  mask: true,
			});
			return false
			uni.showModal({
				title: '确认退款订单',
				content: '你确定要退款订单吗？',
				success() {
					setorder(index,type)
				}
			})
		}
	}
	// 订单操作
	async function setorder(index,type){
		let id = state.list[index].id
		const {code,data,msg} = await sheep.$api.zqzApi.setOrder({
			token:uni.getStorageSync('token'),
			id:id,
			do_type:type,
		})
		if(code=='1'){
			newsDatas()
		}else{
			uni.showToast({
			  title: msg,
			  icon: "none",
			  duration: 1000,
			})
		}
	}
	// 支付订单
	async function zhifu(index){
		let id = state.list[index].id
		let payType=state.list[index].pay_type_text
		let pay_type = ''
		let method =''
		let codes=''
		let port = uni.getSystemInfoSync().uniPlatform 
		console.log(port,'ssss')
		if(port=='windows'){
			method = 'web'
		}
		if(port=='devtools'){
			method = 'miniapp'
		}
		if(port=='mp-weixin'){
			method = 'miniapp'
		}
		if(port=='app'||port=='android'||port=='ios'){
			method = 'app'
		}
		if(payType=='微信'){
			pay_type = 'wechat'
		}else{
			pay_type = 'alipay'
		}
		console.log('codescodescodescodes’')
		if(port=='devtools'||port=='mp-weixin'){
			console.log('codescodescodescodes’')
			// 微信小程序支付
			uni.login({
				"provider": "weixin",
				"onlyAuthorize": true, // 微信登录仅请求授权认证
				success: function(event){
					codes = event.code
					console.log(event.code,'codes’')
					getWei(pay_type,method,codes,id)
				},
				fail: function (err) {
			        // 登录授权失败  
			        // err.code是错误码
			    }
			})
			
		}else{
			const {code,data,msg} = await sheep.$api.zqzApi.setOrder({
				token:uni.getStorageSync('token'),
				id:id,
				do_type:'payment',
				pay_type:pay_type,
				method:method
			})
			if(code=='1'){
				newsDatas()
			}else{
				uni.showToast({
				  title: msg,
				  icon: "none",
				  duration: 1000,
				})
			}
		}
		
		
	}
	//微信小程序支付
	async function getWei(pay_type,method,codes,id){
		const {code,data,msg} = await sheep.$api.zqzApi.setOrder({
			token:uni.getStorageSync('token'),
			id:id,
			do_type:'payment',
			pay_type:pay_type,
			method:method,
			code:codes
		})
		if (code == 1) {
			let orderInfo=data
		    uni.requestPayment({
		       provider: 'wxpay',    //支付类型-固定值
		       timeStamp:orderInfo.timeStamp, // 时间戳（单位：秒）
		       nonceStr: orderInfo.nonceStr, // 随机字符串
		       package: orderInfo.package, // 固定值
		       signType:orderInfo.signType, //固定值
		       paySign:orderInfo.paySign, //签名
		       success: function (res) {
		           console.log('success:' + JSON.stringify(res));
		           console.log("支付成功");
		       },
		       fail: function (err) {
		           console.log('fail:' + JSON.stringify(err));
		           console.log("支付失败");
		       }
		   });
		}
	}
	async function setPrice(){
		let id = state.list[state.index].id
		const {code,data,msg} = await sheep.$api.zqzApi.setOrder({
			token:uni.getStorageSync('token'),
			id:id,
			do_type:'edit',
			amount:state.amount,
		})
		if(code==1){
			state.showPrice=false
			newsDatas()
		}else{
			uni.showToast({
			  title: msg,
			  icon: "none",
			  duration: 1000,
			})
		}
	}
	function close(){
		state.showPrice=false
	}
	function newsDatas(){
		state.page=1
		state.hasMore=true
		state.isLoad=false
		state.list=[]
		getGoodsList()
	}
	async function getGoodsList(){
		
		
		if(state.isLoad) return false
		if(!state.hasMore) return false
		state.isLoad=true
		const {code,data} = await sheep.$api.zqzApi.getOrderList({
			token:uni.getStorageSync('token'),
			order_type:state.order_type,
			status:state.navid,
			page:state.page,
			limit:state.pageSize,
		})
		if (code == 1) {
			let list = data.list
			let hasmore = list.length===state.pageSize
			list = state.list.concat(list)
			state.hasMore = hasmore
			state.list = list
			state.isLoad = false
			state.page = state.page+1
		}
	}
	
	function getNav(id){
		state.navid=id
		newsDatas()
	}
</script>

<style lang="scss">
	.priceBox{
		width: calc(100% - 100rpx);
		height: auto;
		padding: 50rpx;
	}
	.priceBox .input{
		width: 80%;
		height: 60rpx;
		margin: 0 auto;
		border: 1rpx solid #d1d1d1;
		padding:12rpx 32rpx;
		border-radius: 32rpx;
	}
	.priceBox .input input{
		width: 100%;
		height: 60rpx;
		
	}
	.priceBox .isPrice{width: 300rpx; height: 60rpx; background: #1BDDD0; color: #fff; border-radius: 60rpx; display: flex; align-items: center; justify-content: center; margin: 0 auto; margin-top: 50rpx;}
	.center{
		width: calc(100% - 64rpx);
		margin: 20rpx auto 0;
		.lieones{
			width:calc(100% - 48rpx);
			padding:24rpx;
			background: #fff;
			border-radius: 12rpx;
			margin-bottom:20rpx;
			.topen{
				width: 100%;
				display: flex;
				align-items: center;
				justify-content:space-between;
				font-size:28rpx;
				margin:20rpx auto;
				.hao{
					
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #999999;
				}
				.types{
					color: #1BDDD0;
				}
				.wancheng{
					color: #999999;
				}
			}
			.tuDing{
				display: flex;
				// align-items: center;
				.imges{
					width: 100rpx;
					margin-right: 20rpx;
					image{
						width: 100rpx;
						height:100rpx;
						border-radius: 12rpx;
					}
				}
				.rights{
					width: 80%;
				}
				.tites{
					font-size:28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;
					
				}
				.titemo{
					width: 100%;
					font-weight: bold;
					font-size: 32rpx;
					margin-top:20rpx;
					display: flex;
					justify-content:space-between;
					align-items:center;
					.tuikuan{
						color:#1BDDD0;
						font-size: 28rpx;
						// font-weight: 100;
					}
				}
			}
			.orderDing{
				display: flex;
				justify-content:flex-end;
				font-size: 28rpx;
				.xiuJia{
					width:152rpx;
					line-height:60rpx;
					text-align: center;
					border-radius:32rpx;
					opacity: 1;
					color: #1BDDD0;
					border: 1px solid #1BDDD0;
					margin-left: 10rpx;
				}
				.quXiao{
					width:152rpx;
					margin-left: 10rpx;
					line-height:60rpx;
					text-align: center;
					border-radius:32rpx;
					opacity: 1;
					color: #999999;
					border: 1px solid #DBDBDB;
				}
				.tui{
					margin-left: 10rpx;
					width:152rpx;
					line-height:60rpx;
					text-align: center;
					border-radius:32rpx;
					opacity: 1;
					color: #999999;
					border: 1px solid #DBDBDB;
				}
			}
			
		}
	}
    page {
		height: 100%;
		width: 100%;
		background-color: #F5F5F5;
		.main {
			height: 100%;
			width: 100%;
			background-color: #F5F5F5;
		}
		.tanNav{
			width: calc(100% - 64rpx);
			padding:32rpx;
			display: flex;
			align-items: center;
			justify-content:space-between;
			background: #fff;
			.ones{
				line-height: 50rpx;
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;
				
			}
			.onesmo{
				line-height: 50rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: bold;
				color: #333333;
				background: url(../../static/me_icon_dd_t@2x.png) no-repeat bottom;
				background-size: 100%;
				// background: linear-gradient(180deg, rgba(27,221,208,0.5) 0%, rgba(27,221,208,0) 100%);
				border-radius:10rpx;
			}
		}
    }
</style>
